package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.framework.util.GuavaCacheUtil;
import com.lechi.web.service.IIotTriggerRulesService;
import com.lechi.web.system.domain.IotTriggerRules;
import com.lechi.web.system.domain.IotTriggerRulesParse;
import com.lechi.web.system.mapper.iot.IotTriggerRulesMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 数据分析规则Service业务层处理
 *
 * @author yangguang
 * @date 2024 -05-10
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class IotTriggerRulesServiceImpl implements IIotTriggerRulesService {

    private final IotTriggerRulesMapper iotTriggerRulesMapper;

    /**
     * 查询数据分析规则
     *
     * @param id 数据分析规则主键
     * @return 数据分析规则
     */
    @Override
    public IotTriggerRules selectIotTriggerRulesById(Long id) {
        return iotTriggerRulesMapper.selectIotTriggerRulesById(id);
    }

    /**
     * 查询数据分析规则列表
     *
     * @param iotTriggerRules 数据分析规则
     * @return 数据分析规则
     */
    @Override
    public List<IotTriggerRules> selectIotTriggerRulesList(IotTriggerRules iotTriggerRules) {
        try {
            return iotTriggerRulesMapper.selectIotTriggerRulesList(iotTriggerRules);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询数据分析规则列表", method, iotTriggerRules));
            throw new ServiceException(ErrorCodeEnum.UPDATE_ERROR.getValue(), ErrorCodeEnum.UPDATE_ERROR.getKey());
        }
    }

    /**
     * 缓冲触发器规则，查询列表
     *
     * @param deviceId 数据分析规则
     * @return 数据分析规则
     */
    @Override
    public List<IotTriggerRulesParse> selectIotTriggerRulesForCache(Long deviceId) {
        try {
            return iotTriggerRulesMapper.selectIotTriggerRulesForCache(deviceId);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "缓冲触发器规则，查询列表", method, deviceId));
            throw new ServiceException(ErrorCodeEnum.UPDATE_ERROR.getValue(), ErrorCodeEnum.UPDATE_ERROR.getKey());
        }
    }

    /**
     * 新增数据分析规则
     *
     * @param iotTriggerRules 数据分析规则
     */
    @Override
    public void insertIotTriggerRules(IotTriggerRules iotTriggerRules) {
        try {
            iotTriggerRulesMapper.insertIotTriggerRules(iotTriggerRules);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "新增触发器规则", method, iotTriggerRules));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 修改数据分析规则
     *
     * @param iotTriggerRules 数据分析规则
     */
    @Override
    public void updateIotTriggerRules(IotTriggerRules iotTriggerRules) {
        try {
            iotTriggerRulesMapper.updateIotTriggerRules(iotTriggerRules);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "修改触发器规则", method, iotTriggerRules));
            throw new ServiceException(ErrorCodeEnum.UPDATE_ERROR.getValue(), ErrorCodeEnum.UPDATE_ERROR.getKey());
        }
    }

    /**
     * 批量删除数据分析规则
     *
     * @param ids 需要删除的数据分析规则主键
     */
    @Override
    public void deleteIotTriggerRulesByIds(Long[] ids) {
        try {
            iotTriggerRulesMapper.deleteIotTriggerRulesByIds(ids);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "批量删除触发器规则", method, ids));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }


    }

    /**
     * 批量删除数据分析规则
     *
     * @param deviceId 需要删除的数据分析规则主键
     */
    @Override
    public void deleteIotTriggerRulesByDeviceId(Long deviceId) {
        try {
            iotTriggerRulesMapper.deleteIotTriggerRulesByDeviceId(deviceId);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "根据设备ID删除触发器规则", method, deviceId));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }

    /**
     * 批量删除数据分析规则
     *
     * @param sensorAliasId 需要删除的数据分析规则主键
     */
    @Override
    public void deleteIotTriggerRulesBySensorAliasId(Long sensorAliasId) {
        try {
            iotTriggerRulesMapper.deleteIotTriggerRulesBySensorAliasId(sensorAliasId);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "根据设备配置的传感器ID删除触发器规则", method, "传感器关系表ID:" + sensorAliasId));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }

    @Override
    public void cleanCacheAlarmRule(IotTriggerRules iotTriggerRules) {
        final List<IotTriggerRules> iotTriggerRulesList = iotTriggerRulesMapper.selectIotTriggerRulesList(iotTriggerRules);
        iotTriggerRulesList.forEach(iotTriggerRule -> {
            GuavaCacheUtil.getAlarmRuleMap().remove("AlarmRule_Id_" + iotTriggerRule.getDeviceId() + "_" + iotTriggerRule.getSensorAliasId());
        });
    }
}
